package com.example.backend_422.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.backend_422.common.Result;
import com.example.backend_422.entity.House;
import com.example.backend_422.mapper.HouseMapper;
import com.example.backend_422.records.HouseRecord;
import com.example.backend_422.service.HouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import freemarker.template.utility.ClassUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.math.BigDecimal;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwyzsn
 * @since 2023-04-22
 */
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements HouseService {

    @Autowired // 自动装配, 通过 @Autowired 自动装配 HouseMapper
    private HouseMapper houseMapper;

    /**
     * 获取所有房屋信息
     * @return
     */
    public Result getAll() {
        return Result.succ(houseMapper.selectList(null));
    }

    /**
     * 根据房屋id获取房屋信息
     * @param houseId
     * @return
     */
    public Result getMyHouse(Integer landlordId) {
        QueryWrapper<House> wrapper = new QueryWrapper<>();// 构造查询条件
        wrapper.eq("landlord_id",landlordId); // 查询条件 landlord_id = landlordId
        return Result.succ(houseMapper.selectList(wrapper)); // 返回查询结果
    }

    /**
     * 根据房屋id删除 房屋信息
     * @param houseId
     * @return
     */
    public Result deleteByHouseId(Integer houseId) {
        QueryWrapper<House> wrapper = new QueryWrapper<>(); // 构造查询条件
        wrapper.eq("house_id",houseId); // 查询条件 house_id = houseId
        houseMapper.delete(wrapper); // 删除
        return Result.succ("null");
    }

    public Result addNewHouse(HouseRecord houseRecord) {
//        System.out.println(houseRecord.pic());
        House house = new House(); // 构造一个 House 对象

        if(houseRecord.pic() != null){
            // 如果上传了图片, 则保存图片
            UUID uuid = UUID.randomUUID();// 生成一个随机的 UUID
            String filename = uuid.toString().replaceAll("-",""); // 去掉 UUID 中的 -
            String originalFilename = houseRecord.pic().getOriginalFilename(); // 获取上传的文件名
            String suffix = originalFilename.substring(originalFilename.lastIndexOf(".")); // 获取文件后缀名
            String newFilename = filename + suffix; // 新的文件名
            String path = this.getClass().getClassLoader().getResource("").getPath().toString()+"public/" + newFilename; // 保存路径
            File file = new File(path); // 构造一个文件
            try {
                // 保存文件
                houseRecord.pic().transferTo(file);
                house.setPic("http://localhost:8085/static/"+newFilename);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(houseRecord.houseName() != null){
            // 如果上传了房屋名称, 则保存房屋名称
            house.setName(houseRecord.houseName());
        }
        if(houseRecord.address() != null){
            // 如果上传了房屋地址, 则保存房屋地址
            house.setAddress(houseRecord.address());
        }
        if(houseRecord.area() != null){
            // 如果上传了房屋面积, 则保存房屋面积
            house.setArea(Float.parseFloat(houseRecord.area()));
        }
        if(houseRecord.description() != null){
            // 如果上传了房屋描述, 则保存房屋描述
            house.setDescription(houseRecord.description());
        }
        if(houseRecord.landlordId() != null){
            // 如果上传了房东id, 则保存房东id
            house.setLandlordId(houseRecord.landlordId());
        }
        if(houseRecord.landlordName() != null){
            // 如果上传了房东姓名, 则保存房东姓名
            house.setLandlordName(houseRecord.landlordName());
        }
        if(houseRecord.landlordPhone() != null){
            // 如果上传了房东电话, 则保存房东电话
            house.setLandlordPhone(houseRecord.landlordPhone());
        }
        if(houseRecord.powerPrice() != null){
            // 如果上传了电费, 则保存电费
            house.setPowerPrice(BigDecimal.valueOf(Float.parseFloat(houseRecord.powerPrice())));
        }
        if(houseRecord.rentPrice() != null){
            // 如果上传了租金, 则保存租金
            house.setRentPrice(BigDecimal.valueOf(Float.parseFloat(houseRecord.rentPrice())));
        }
        if(houseRecord.status() != null){
            // 如果上传了房屋状态, 则保存房屋状态
            house.setStatus(houseRecord.status());
        }
        if(houseRecord.waterPrice() != null){
            // 如果上传了水费, 则保存水费
            house.setWaterPrice(BigDecimal.valueOf(Float.parseFloat(houseRecord.waterPrice())));
        }
        houseMapper.insert(house); // 保存
        return Result.succ("null"); // 返回
    }

    public Result updateHouse(Integer houseId, HouseRecord houseRecord) {
        // 构造查询条件
        UpdateWrapper<House> wrapper = new UpdateWrapper<>();
        wrapper.eq("house_id",houseId);// 查询条件 house_id = houseId
        // 剩下的和添加房屋信息一样，就不写了
        if(houseRecord.pic() != null){
            UUID uuid = UUID.randomUUID();
            String filename = uuid.toString().replaceAll("-","");
            String originalFilename = houseRecord.pic().getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFilename = filename + suffix;

            String path = this.getClass().getClassLoader().getResource("").getPath().toString()+"public/" + newFilename;
            File file = new File(path);
            try {
                houseRecord.pic().transferTo(file);
                wrapper.set("pic","http://localhost:8085/static/"+newFilename);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(houseRecord.houseName() != null){
            wrapper.set("name",houseRecord.houseName());
        }
        if(houseRecord.address() != null){
            wrapper.set("address",houseRecord.address());
        }
        if(houseRecord.area() != null){
            wrapper.set("area",Float.parseFloat(houseRecord.area()));
        }
        if(houseRecord.description() != null){
            wrapper.set("description",houseRecord.description());
        }
        if(houseRecord.landlordId() != null){
            wrapper.set("landlord_id",houseRecord.landlordId());
        }
        if(houseRecord.landlordName() != null){
            wrapper.set("landlord_name",houseRecord.landlordName());
        }
        if(houseRecord.landlordPhone() != null){
            wrapper.set("landlord_phone",houseRecord.landlordPhone());
        }
        if(houseRecord.powerPrice() != null){
            wrapper.set("power_price",BigDecimal.valueOf(Float.parseFloat(houseRecord.powerPrice())));
        }
        if(houseRecord.rentPrice() != null){
            wrapper.set("rent_price",BigDecimal.valueOf(Float.parseFloat(houseRecord.rentPrice())));
        }
        if(houseRecord.status() != null){
            wrapper.set("status",houseRecord.status());
        }
        if(houseRecord.waterPrice() != null){
            wrapper.set("water_price",BigDecimal.valueOf(Float.parseFloat(houseRecord.waterPrice())));
        }
        houseMapper.update(null,wrapper);
        return Result.succ("null");
    }
}
